﻿using System;
using System.Text;
using System.Data;
using System.Collections;
using System.Windows.Forms;

using WP.Commons;
using WP.DB;
using WP.RM.DataAccess;
using WP.RM.DataEntity;

namespace WP.RM.BusinessRule
{
    public class RMCategoryBRL : RuleBase
    {
        public RMCategoryBRL()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        #region Insert rmCategory Valid
        public bool InsertValid(DBAccess dBAccess, RMCategoryDEL  rmCategoryDEL)
        {
            bool bReturn = true;
            string strErrPrompt = string.Empty;
            SysVar.ErrorMessage.Clear();

            DataView dvwCheck = dBAccess.GetDataTable("select * from mnamedict where NeedCheck=1 and tablename='" + RMCategoryDEL.TABLENAME_RMCategroy + "'").DefaultView;
            foreach (DataRowView drw in dvwCheck)
            {
                if (!FieldCheck(dBAccess, rmCategoryDEL.Tables[RMCategoryDEL.TABLENAME_RMCategroy].Rows[0], drw["FieldName"].ToString(), ref strErrPrompt, FormStatus.AddNew))
                {
                    bReturn = false;
                    if (SysVar.ErrorMessage.Count == 0)
                    {
                        SysVar.ErrorMessage.Add(new ErrInfo(strErrPrompt, drw["FieldName"].ToString()));
                    }
                    break;
                }
            }
            return bReturn;
        }
        #endregion

        #region Update rmCategory Valid
        public bool UpdateValid(DBAccess dBAccess, RMCategoryDEL  rmCategoryDEL)
        {
            bool bReturn = true;
            string strErrPrompt = string.Empty;
            SysVar.ErrorMessage.Clear();

            if (IsExistedAndNotChanged(dBAccess, rmCategoryDEL.Tables[RMCategoryDEL.TABLENAME_RMCategroy]))
            {
                DataView dvwCheck = dBAccess.GetDataTable("select * from mnamedict where NeedCheck=1 and tablename='" + RMCategoryDEL.TABLENAME_RMCategroy + "'").DefaultView;
                foreach (DataRowView drw in dvwCheck)
                {
                    if (!FieldCheck(dBAccess, rmCategoryDEL.Tables[RMCategoryDEL.TABLENAME_RMCategroy].Rows[0], drw["FieldName"].ToString(), ref strErrPrompt, FormStatus.Update))
                    {
                        bReturn = false;
                        if (SysVar.ErrorMessage.Count == 0)
                        {
                            SysVar.ErrorMessage.Add(new ErrInfo(strErrPrompt, drw["FieldName"].ToString()));
                        }
                        break;
                    }
                }
            }
            else
            {
                bReturn = false;
            }
            return bReturn;
        }
        #endregion

        #region Delete MCity Valid
        public bool DeleteValid(DBAccess dBAccess, string strRecId, byte[] btOldRecStamp)
        {
            SysVar.ErrorMessage.Clear();

            if (!base.IsExistedAndNotChanged(dBAccess, "rmCategory", strRecId, btOldRecStamp))
                return false;
            if (MsgBoxEx.Show(Msg.Select("您确定要删除此记录?", DataFunctions.GetMsg("SureToDeleteThisRecord")), SysVar.ConfirmTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
            {
                return false;
            }
            string strCategoryName = DataFunctions.GetFieldValue ("rmCategory","CategoryName","RecID='"+ strRecId  +"'");
            string strSQL = "select * from rmCategorySetBOMHdr where CategoryName= '" + strCategoryName + "'";
            DataRow drRow = dBAccess.GetDataFirstRow(strSQL);
            if ( drRow !=null )
            {
                MsgBoxEx.Show("已经使用，不能删除此记录！", SysVar.WarnTitle);
                return false; 
            }
            return true;
        }
        #endregion

        #region Field Check
        public bool FieldCheck(DBAccess dbcon, DataRow dr, string strFieldName, ref string strErrPrompt, FormStatus formStatus)
        {
            strErrPrompt = string.Empty;
            if (strFieldName == null) return true;

            bool blnExistsField = false;
            for (int i = 0; i < dr.Table.Columns.Count; i++)
            {
                if (dr.Table.Columns[i].Caption.ToUpper().Trim() == strFieldName.Trim().ToUpper())
                {
                    blnExistsField = true;
                    break;
                }
            }
            if (!blnExistsField) return true;
            string strFieldValue = CvtFunc.ToString(dr[strFieldName.Trim()]);

            switch (strFieldName.ToUpper().Trim())
            {
                #region CATEGORYID
                case "CATEGORYNAME":
                case "CATEGORYID":
                    if (formStatus == FormStatus.AddNew)
                        return RuleBase.FieldValueCheckOne(dbcon, strFieldValue, RMCategoryDEL.TABLENAME_RMCategroy, strFieldName, ref  strErrPrompt);
                    else
                        return true;
                #endregion
                default:
                    return RuleBase.FieldValueCheckOne(dbcon, strFieldValue, RMCategoryDEL.TABLENAME_RMCategroy, strFieldName, ref strErrPrompt);
            }
        }
        #endregion

    }
}